//
//  ExchangeModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import Gzip
class ExchangeModel: BaseModel {
    var depModel:DepthModel? //深度model
    
    var klinDataArr:Array<Array<Float>> = [Array<Float>]() //K线数据源
    
    
    var depathSort:DepathSort?//算法类
    
    override init() {
        super.init()
        let depath = DepathSort()
        self.depathSort = depath
    }
    
}
extension ExchangeModel {
    
    func convertStringToDictionary(text: String) -> [String:AnyObject]? {
        if let data = text.data(using: String.Encoding.utf8) {
            do {
                return try JSONSerialization.jsonObject(with: data, options: [JSONSerialization.ReadingOptions.init(rawValue: 0)]) as? [String:AnyObject]
            } catch let error as NSError {
            
            }
        }
        return nil
    }
    
    
    
    
    func convertDictionaryToString(dict:[String:AnyObject]) -> String {
        var result:String = ""
        do {
            //如果设置options为JSONSerialization.WritingOptions.prettyPrinted，则打印格式更好阅读
            let jsonData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.init(rawValue: 0))
            
            if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
                result = JSONString
            }
            
        } catch {
            result = ""
        }
        return result
    }
    
    func convertArrayToString(arr:[AnyObject]) -> String {
        var result:String = ""
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: arr, options: JSONSerialization.WritingOptions.init(rawValue: 0))
            
            if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
                result = JSONString
            }
            
        } catch {
            result = ""
        }
        return result
    }
    
    
    func OrganizeData(dic:Dictionary<String, Any>) -> Bool {
        
//        let dic:Dictionary = self.convertStringToDictionary(text: text)!
//        let str:String = dic["cmd"] != nil ? dic["cmd"] as! String : "";
//        let string:String = String(format: "exchange_%@", str)
//        switch string {
//        case exchangeStyle.exchangeTick.rawValue:
//            if let jsonData = text.data(using: String.Encoding.utf8) {
//                //                let model = try? JSONDecoder().decode(TickModel.self, from: jsonData)
//                //                ExchangeWebScoket.manger.webScoketDic[string] = model as AnyObject
//            }
//            break
//        case exchangeStyle.exchangeDepth.rawValue:
//            if DepthModel(JSONString: text) == nil {
//                return false
//            }
//            self.depModel = DepthModel(JSONString: text)
//            if self.depModel?.data == nil {
//                return false
//            }
//            let decompressedData = self.gzip(data: (self.depModel?.data)!)
//            if decompressedData == nil {
//                return false
//            }
//            let originalStr =  String(data: decompressedData!, encoding: String.Encoding.utf8)
//            let modelDic = self.convertStringToDictionary(text: originalStr!)
//            if modelDic == nil {
//                return false
//            }
//            self.depModel?.metadata = modelDic!
//            //整理上下游数据
//            self.depathSort?.sortDepth(model: self.depModel!,digit: 2)
//            break
//        case exchangeStyle.exchangeChart.rawValue:
//            let dic = self.convertStringToDictionary(text: text)
//            if dic!["data"] == nil {
//                return false
//            }
//            let decompressedData = self.gzip(data: (dic!["data"] as! String?)!)
//            if decompressedData == nil {
//                
//                return false
//            }
//            let deconder = JSONDecoder()
//            do {
//                self.klinDataArr = try deconder.decode(
//                    Array<Array<Float>>.self, from: decompressedData!)
//                
//            } catch  {
//                print("解析失败:\(error)")
//            }
//            break
//        default:
//            break
//        }
        return true
    }
    
    func gzip(data:String) -> Data? {
        let base64Data = NSData(base64Encoded:data, options:NSData.Base64DecodingOptions(rawValue: 0))
        if base64Data == nil {
            return nil
        }
        let data = base64Data! as Data
        
        let decompressedData: Data
        if (data.isGzipped) {
            decompressedData = (try! data.gunzipped())
        } else {
            return nil
        }
        return decompressedData
    }

    
}
